#! /bin/sh
#
# Run Scapy test suite.
#
# If ran with no arguments:
#   ./run_tests
# this util will run the test suite using tox, with options that should work
# regardless of the platform or the dependencies. The only dependency for this
# to work are python3 (or python) and tox.
#
# If ran with arguments, this will call UTscapy.py
#
# ATTENTION PACKAGE MAINTAINERS:
# If you do need to run Scapy tests, calling ./run_tests should be enough.
#
DIR=$(dirname "$0")/..
if [ -z "$PYTHON" ]
then
  ARGS=""
  for arg in "$@"
  do
    case $arg
    in
      -2) PYTHON=python2;;
      -3) PYTHON=python3;;
       *) ARGS="$ARGS $arg";;
    esac
  done
  PYTHON=${PYTHON:-python3}
fi
$PYTHON --version > /dev/null 2>&1
if [ ! $? -eq 0 ]
then
  echo "WARNING: '$PYTHON' not found, using 'python' instead."
  PYTHON=python
fi

if [ -z "$ARGS" ]
then
  # No arguments specified: use tox
  # We use flags to disable tests that use external non tox-installed
  # software.

  # Check tox
  tox --version >/dev/null 2>/dev/null
  if [ ! $? -eq 0 ]
  then
    echo "ERROR: tox is not installed."
    echo "You can still run ./run_tests with arguments: see ./run_tests -h"
    echo "e.g. ./run_tests -t tls.uts -F"
    exit 1
  fi

  # Run tox
  export UT_FLAGS="-K tcpdump -K manufdb -K wireshark -K ci_only"
  export SIMPLE_TESTS="true"
  PYVER=$($PYTHON -c "import sys; print('.'.join(sys.version.split('.')[:2]))")
  ${DIR}/.config/ci/test.sh $PYVER non_root
  exit $?
fi
PYTHONPATH=$DIR exec "$PYTHON" ${DIR}/scapy/tools/UTscapy.py $ARGS
